$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'promotion/configcouponucenter/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true ,hidden:true },
			{ label: '优惠券id', name: 'couponId', index: 'coupon_id', width: 40 ,align: 'center'},
			{ label: '优惠券标题', name: 'couponTitel', index: 'coupon_titel', width: 180 },
			{ label: '用户获取总量', name: 'couponGenQuantity', index: 'coupon_gen_quantity', width: 80,align: 'center' },
			{ label: '发放总量上限', name: 'couponMaxGive', index: 'coupon_max_give', width: 80,align: 'center' },
			{ label: '渠道类型', name: 'couponType', index: 'coupon_type', width: 80,align: 'center',
                formatter: function (value, options, row) {
                    return value === 0 ? '领卷中心' : (value===1?'积分兑换':'活动发券');
                }},
			{ label: '兑换优惠券积分', name: 'couponPoint', index: 'coupon_point', width: 80,align: 'center' },
			{ label: '优惠券兑换数量', name: 'exchangeNum', index: 'exchange_num', width: 80,align: 'center' },
			{ label: '间隔天数', name: 'intervalTime', index: 'interval_time', width: 50,align: 'center' },
			{ label: '是否发放', name: 'couponGive', index: 'coupon_give', width: 80 ,align: 'center',
                formatter: function (value, options, row) {
                    return value === 1 ? '上架' : '下架';
                }},
			{ label: '操作人员', name: 'operation', index: 'operation', width: 80,align: 'center' },
			{ label: '创建时间', name: 'createTime', index: 'create_time', width: 80 ,align: 'center'},
			{ label: '更新时间', name: 'updayeTime', index: 'updaye_time', width: 80,align: 'center' }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount",
            userdata: "userData"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
            vm.couponList = $("#jqGrid").jqGrid('getGridParam', 'userData').couponList;
        }
    });
    /* oss上传 */
    $.files_up_oss({
        id: ["uploadImage"],//按钮id
        url: "/sys/oss/getSignature",
        dir: "config/couponucenter/",
        file_type: "jpg,png,gif,psd,ai,cdr,eps,ppt,word,excel,pdf,tiff,rar,zip,7z,gif,AVI,WMV,RM,RMVB,MPEG1,MPEG2,MPEG4,3GP,ASF,SWF,VOB,DAT,MOV,M4V,FLV,F4V,MKV,TIF",
        size: "100mb",//mb
        view_url: window.parent.vm.ossHost
    }, function (index_number, json) {
        if (json.Status == 'OK') {
            Vue.set(vm.configCouponUcenter, 'imageUrl', json.filename);
        }
    })
});



var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		configCouponUcenter: {},
        showImageUrl: null, // 展示图片
        q:{
            couponTitel:'' //优惠券标题
        },
        couponList:[],//优惠券信息
	},
	methods: {
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.configCouponUcenter = {
                couponGive:1,
                couponType:0,
                exchangeNum:1
            };
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
            if (!vm.configCouponUcenter.couponId ||!vm.configCouponUcenter.couponMaxGive) {
            	alert('必填选不能为空!')
            	return
            }
            if (vm.configCouponUcenter.couponType == 1) {
                if (!vm.configCouponUcenter.couponPoint || !vm.configCouponUcenter.exchangeNum ||!vm.configCouponUcenter.imageUrl) {
                    alert('必填选不能为空!')
                    return
                }
            }
		    //所有用户获取总数量记录>发放总数量上限
		    if (vm.configCouponUcenter.couponGenQuantity>vm.configCouponUcenter.couponMaxGive) {
                alert("发放总数量上限不能少于所有用户获取总数量");
                return;
            }
			var url = vm.configCouponUcenter.id == null ? "promotion/configcouponucenter/save" : "promotion/configcouponucenter/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.configCouponUcenter),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "promotion/configcouponucenter/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "promotion/configcouponucenter/info/"+id, function(r){
                vm.configCouponUcenter = r.configCouponUcenter;
            });
		},
		reload: function (event) {
			vm.showList = true;
			$("#jqGrid").jqGrid('setGridParam',{
                postData: {//postData此数组内容直接赋值到url上，参数类型：{name1:value1…}
                    'couponTitel': vm.q.couponTitel.trim()
                },
                page:1
            }).trigger("reloadGrid");
		},
        reset(){
            vm.q.couponTitel = '';
            vm.reload();
        },
        query(){
            vm.reload();
        }
	}
});